package lib.bone.flow.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Validator;
import lib.bone.flow.domain.entity.FlowTaskAssign;
import lib.bone.flow.dao.FlowTaskAssignDAO;
import lib.bone.flow.domain.dto.FlowTaskAssignAddDTO;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.flow.domain.entity.FlowDefine;
import lib.bone.flow.service.FlowDefineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author kindear
 */
@Slf4j
@Service
public class FlowTaskAssignService {
    @Autowired
    FlowTaskAssignDAO flowTaskAssignDAO;

    @Autowired
    FlowDefineService flowDefineService;

    public PageResultDTO<FlowTaskAssign> getAll(Pageable pageable, Example<FlowTaskAssign> example) {
        Page<FlowTaskAssign> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = flowTaskAssignDAO.findAll(pageable);
        }else {
            page = flowTaskAssignDAO.findAll(example,pageable);
        }
        List<FlowTaskAssign> list = page.getContent();
        PageResultDTO<FlowTaskAssign> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;
    }

    /**
     * 新增流程审核指派
     * @param dto
     * @return
     */
    public ResponseDTO<FlowTaskAssign> saveOne(FlowTaskAssignAddDTO dto){
        Optional<FlowDefine> defineOp = flowDefineService.getByDefineCode(dto.getFlowDefineCode());
        if (!defineOp.isPresent()) {
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"没有该流程定义");
        }
        FlowTaskAssign flowTaskAssign = new FlowTaskAssign();
        Boolean isExist = flowTaskAssignDAO.existsByFlowDefineCodeAndFlowNodeIdAndAssignTypeAndAssignId(
                dto.getFlowDefineCode(),
                dto.getFlowNodeId(),
                dto.getAssignType(),
                dto.getAssignId()
        );
        if (isExist){
            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN,"已存在相同记录");
        }
        BeanUtil.copyProperties(dto,flowTaskAssign, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(flowTaskAssignDAO.save(flowTaskAssign));

    }

    public Optional<FlowTaskAssign> getById(String id){
        return flowTaskAssignDAO.findById(id);
    }

    public Optional<FlowTaskAssign> getByAssignId(String assignId){
        return flowTaskAssignDAO.findByAssignId(assignId);
    }

    /**
     * 查询对应的分配信息
     */
    public List<FlowTaskAssign> getLinkAssign(String flowDefineCode,String flowNodeId){
        return flowTaskAssignDAO.findByFlowDefineCodeAndFlowNodeId(flowDefineCode,flowNodeId);
    }

    public void deleteAssign(String assignId){
        flowTaskAssignDAO.deleteById(assignId);
    }
}
